﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CTRL;
using DAL;
using System.Threading;

namespace GUI_Administration
{
    public partial class GUIBatteryItem : Form
    {
        private static GUIBatteryItem instance;
        private CtrlBatteryItem ctrlBatItem;

        private GUIBatteryItem()
        {
            InitializeComponent();

            ctrlBatItem = CtrlBatteryItem.Instance;

            UpdateList();
        }

        public static GUIBatteryItem Instance
        {
            get
            {
                if (instance == null)
                    instance = new GUIBatteryItem();

                return instance;
            }
        }

        private void UpdateList()
        {
            dataGridView.Rows.Clear();

            List<BatteryItem> list = ctrlBatItem.GetAllBatteryItems().ToList();

            foreach (BatteryItem item in list)
                dataGridView.Rows.Add(item.batteryID, item.modelID);
        }

        private void getBtn_Click(object sender, EventArgs e)
        {
            if (int.Parse(findTxtBox.Text) == 0)
                UpdateList();
            else
            {
                dataGridView.Rows.Clear();
                BatteryItem item = ctrlBatItem.GetBatteryItem(int.Parse(findTxtBox.Text));
                dataGridView.Rows.Add(item.batteryID, item.modelID);
            }
            findTxtBox.Clear();
        }

        private void addBtn_Click(object sender, EventArgs e)
        {
            ctrlBatItem.CreateBatteryItem(int.Parse(dataGridView.CurrentRow.Cells[1].Value.ToString()),
                dataGridView.CurrentRow.Cells[2].Value.ToString(),
                (BatteryState)Enum.Parse(typeof(BatteryState), dataGridView.CurrentRow.Cells[3].Value.ToString()));
            UpdateList();
        }

        private void updateBtn_Click(object sender, EventArgs e)
        {
            ctrlBatItem.UpdateBatteryItem(int.Parse(dataGridView.CurrentRow.Cells[0].Value.ToString()),
                int.Parse(dataGridView.CurrentRow.Cells[1].Value.ToString()),
                dataGridView.CurrentRow.Cells[2].Value.ToString(),
                (BatteryState)Enum.Parse(typeof(BatteryState), dataGridView.CurrentRow.Cells[3].Value.ToString()));
            UpdateList();
        }

        private void deleteBtn_Click(object sender, EventArgs e)
        {
            ctrlBatItem.DeleteBatteryItem(int.Parse(dataGridView.CurrentRow.Cells[0].Value.ToString()));
            UpdateList();
        }

        private void back_Click(object sender, EventArgs e)
        {
            GUIMain main = GUIMain.Instance;
            main.Show();

            this.Hide();
        }
    }
}